package org.exeed.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.exeed.constant.PublishStatusContents;
import org.exeed.dao.ModMapper;
import org.exeed.dao.PageMapper;
import org.exeed.dao.PageSketchMapper;
import org.exeed.entity.ImageEntity;
import org.exeed.entity.ModEntity;
import org.exeed.entity.PageEntity;
import org.exeed.entity.PageSketchEntity;
import org.exeed.utils.JWTUtils;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageSketchService {

    @Autowired
    private PageSketchMapper pageSketchMapper;

    @Autowired
    private SysService sysService;

    @Value("${file.path}")
    private String filepath = "./temp/";

    @Value("${file.tmp}")
    private String tmpPath = "";

    private static String PAGE_COVER_PATH = "page/cover/";

    public boolean save(PageSketchEntity entity) {

        PageSketchEntity currentEntity = entity.getId() == null ? null : pageSketchMapper.selectById(entity.getId());

        if (entity.getType().equals("home")) {
            PageSketchEntity home = selectOneByType("home");
            if (home != null && entity.getId() == null) {
                return false;
            }
        }

        entity.setUpdateTime(DateUtil.current());

        String url = sysService.getDefaultUrl();

        String tmpFile = tmpPath + entity.getThumbnailUrl();
        if (!entity.getThumbnailUrl().contains(sysService.getDefaultUrl()) && !entity.getThumbnailUrl().isEmpty()
                && !entity.getThumbnailUrl().equals("") && FileUtil.exist(tmpFile)) {
            entity.setThumbnailFileName(entity.getThumbnailUrl());
            FileUtil.copy(tmpFile, filepath + PAGE_COVER_PATH + entity.getThumbnailUrl(), true);
            entity.setThumbnailUrl(url + PAGE_COVER_PATH + entity.getThumbnailUrl());
        }
        entity.setUpdateTime(DateUtil.current());

        if (currentEntity == null) {
            entity.setId(JWTUtils.createId());
            entity.setIsPublish(PublishStatusContents.UnPublish);

            pageSketchMapper.insert(entity);
        } else {
            if (entity.getIsPublish().equals(PublishStatusContents.UnPublish))
                entity.setIsPublish(PublishStatusContents.UnPublish);

            pageSketchMapper.updateById(entity);
        }

        return true;
    }

    public PageSketchEntity selectOneByType(String type) {

        QueryWrapper<PageSketchEntity> queryWrapper = new QueryWrapper<>();
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        queryWrapper.orderByDesc("update_time");

        return pageSketchMapper.selectOne(queryWrapper);
    }

    public Map selectList(Integer page, Integer size, String key, String type, Integer isPublish) {

        Page<PageSketchEntity> iPage = new Page<>(page, size);
        QueryWrapper<PageSketchEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "path", "type", "title", "des", "thumbnail_url", "publish_time", "update_time", "is_publish", "page_content", "article_type", "pdf_resource_url");
        if (key != null && !key.equals("")) {
            queryWrapper.like("title", key);
        }
        if (isPublish != null) {
            queryWrapper.eq("is_publish", isPublish);
        }
        if (type != null && !type.equals("")) {
            queryWrapper.eq("type", type);
        }
        queryWrapper.orderByDesc("update_time");
        IPage<PageSketchEntity> list = pageSketchMapper.selectPage(iPage, queryWrapper);
        Map<String, Object> R = new HashMap<>();
        R.put("page", page);
        R.put("size", size);
        R.put("total", list.getTotal());
        R.put("list", list.getRecords());
        return R;
    }

//    public Map selectArticle(Integer page, Integer size, String key, Integer isPublish) {
//
//        Page<PageSketchEntity> iPage = new Page<>(page, size);
//        QueryWrapper<PageSketchEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("id", "path", "title", "des", "thumbnail_url", "is_article", "publish_time", "update_time", "is_publish");
//        if (key != null && !key.equals("")) {
//            queryWrapper.like("name", key);
////            queryWrapper.lambda()
////                    .select(PageSketchEntity.class, i -> !i.getColumn().equals("page_content"));
//        }
//        if (isPublish != null) {
//            queryWrapper.eq("is_publish", isPublish);
//        }
//        queryWrapper.eq("is_article", 1);
//        queryWrapper.orderByDesc("update_time");
//        IPage<PageSketchEntity> list = pageSketchMapper.selectPage(iPage, queryWrapper);
//        Map<String, Object> R = new HashMap<>();
//        R.put("page", page);
//        R.put("size", size);
//        R.put("total", list.getTotal());
//        R.put("list", list.getRecords());
//        return R;
//    }

    public PageSketchEntity selectById(Long id) {
        return pageSketchMapper.selectById(id);
    }

    public PageSketchEntity selectByPath(String path) {
        QueryWrapper<PageSketchEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("path", path);

        return pageSketchMapper.selectOne(queryWrapper);
    }


    public boolean delete(Long id) {
        PageSketchEntity entity = pageSketchMapper.selectById(id);

        //判断是否已发布 未发布允许删除
        if (entity.getIsPublish() == 0) {
            pageSketchMapper.deleteById(entity.getId());
            return true;
        }
        return false;
    }
}
